/*
 * @(#)GroupServiceImpl.java 2012-02-17
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shangjia.common.dao.impl.GroupDao;
import cn.shangjia.common.dao.impl.GroupUserDao;
import cn.shangjia.common.domain.Group;
import cn.shangjia.common.domain.GroupUser;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.shop.service.GroupService;

/**
 *
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @version 1.0, 2012-02-17 10:56:22
 * @since 1.0
 */
@Service
public class GroupServiceImpl implements GroupService{
	
	@Resource
	private GroupUserDao groupUserDao;
	
	@Resource
	private GroupDao groupDao;

	@Override
	public List<Group> findGroupsByUserId(String userId) {
		List<Group> groups = new ArrayList<Group>();
		if (StringUtils.isEmpty(userId)) {
			return groups;
		}
		// 查询当前用户的所有的circle
		Map<String, Object> parames = new HashMap<String, Object>();
		parames.put("user_id", userId);
		Collection<GroupUser> gus = groupUserDao.selectList(parames);
		// 如果没有加入任何圈子，返回空
		if (gus.isEmpty()) {
			return groups;
		}
		for (GroupUser gu : gus) {
			String id = gu.getGroup_id();
			if (StringUtils.isEmpty(id)) 
				continue;
			Group group = groupDao.read(id);
			if (group != null)
				groups.add(group);
		}
		return groups;
	}
	
}
